package com.eds.controller;


import com.eds.pojo.BaseResult;
import com.eds.pojo.CodeEnum;
import com.eds.service.NotificationService;
import com.pojo.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：NotificationController
 * @Author: vvSmile
 * @Date: 2025/04/08
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@RestController
@RequestMapping("/notificationController")
public class NotificationController {
    @Autowired
    private NotificationService notificationService;


    //获取用户的通知列表
    @GetMapping("/getNotificationList")
    public BaseResult<List<Notification>> getNotificationList(@RequestHeader("authorization") String token) {
        List<Notification> notifications = notificationService.getUserNotificationList(token, "message");
        if (notifications == null) {
            return BaseResult.error(CodeEnum.GET_USER_NOTIFICATION_LIST_ERROR);
        }
        return BaseResult.ok("获取用户通知列表成功！", notifications);
    }

    //获取未读通知的数量
    @GetMapping("/getUnreadNotification")
    public BaseResult<Integer> getUnreadNotification(@RequestHeader("authorization") String token) {
        Integer count = notificationService.getUnreadNotification(token, "message");
        return BaseResult.ok("获取未读通知的数量成功！", count);
    }

    //标记为已读
    @GetMapping("/readNotification")
    public BaseResult<Boolean> readNotification(@RequestHeader("authorization") String token, Long notificationId) {
        boolean result = notificationService.readNotification(token, notificationId);
        if (!result) {
            return BaseResult.error(CodeEnum.READ_NOTIFICATION_ERROR);
        }
        return BaseResult.ok("标记为已读成功！", result);
    }

    //删除通知
    @DeleteMapping("/deleteNotification")
    public BaseResult<Boolean> deleteNotification(@RequestHeader("authorization") String token, Long notificationId) {
        boolean result = notificationService.deleteNotificationById(token, notificationId);
        if (!result) {
            return BaseResult.error(CodeEnum.DELETE_NOTIFICATION_ERROR);
        }
        return BaseResult.ok("删除通知成功！", result);
    }
}
